package tr.helper.controller.login;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import tr.helper.CommonUtil;
import tr.helper.UserHolder;
import tr.helper.controller.home.HomeMain;
import tr.helper.controller.sprite.SpriteMain;
import tr.helper.controller.wine.WineMain;
import tr.helper.domain.LoginUser;

public class LoginController implements Initializable {

    private static final Map<String, String> STORAGE = new HashMap<>();

    @FXML
    private TextField username;

    @FXML
    private PasswordField password;

    @FXML
    private CheckBox buyWine;

    @FXML
    private CheckBox mouseSprite;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        STORAGE.put("username", "admin");
        STORAGE.put("password", "1qazxsw2");
    }

    @FXML
    public void doLogin() throws Exception {
        if (mouseSprite.isSelected()) {
            CommonUtil.mode = 3;
        }
        if (buyWine.isSelected()) {
            CommonUtil.mode = 2;
        }
//        if (StringUtils.contains(CommonUtil.getSystemName(), "yeweidongdeMac")) {
//            UserHolder.setSession(new LoginUser(1L, CommonUtil.getSystemName()));
//            loginSuccess();
//            return;
//        }
        if (StringUtils.isBlank(username.getText())) {
            CommonUtil.alert(null, "用户名不能为空");
            return;
        }
        if (StringUtils.isBlank(password.getText())) {
            CommonUtil.alert(null, "密码不能为空");
            return;
        }
        if (!(STORAGE.get("username").equals(username.getText()) && STORAGE.get("password").equals(password.getText()))) {
            CommonUtil.alert(null, "用户名密码错误");
            return;
        }
        UserHolder.setSession(new LoginUser(1L, username.getText()));
        loginSuccess();
    }

    public void loginSuccess() throws Exception {
        LoginMain.stage.close();
        if (CommonUtil.mode == 2) {
            new WineMain().start(new Stage());
        } else if (CommonUtil.mode == 3) {
            new SpriteMain().start(new Stage());
        } else {
            new HomeMain().start(new Stage());
        }
    }

}
